// JavaScript Document

// This script can be used to validate form input
//
// Usage example:
// <form action="action.php" method="post" id="form1" onsubmit="return(check_form('element1', 'required', 'element2', 'required', 'element3', 'required,isNumber,inRange:1000:9999'))">

/*
	Copyright 2011 Tobias Heinzer + Peter Affentranger

	This file is part of the website flugservice.ch.

    The website flugservice.ch is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    The website flugservice.ch is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with the website flugservice.ch.  If not, see <http://www.gnu.org/licenses/>.
*/


// Form validating function
//
// Arguments syntax for function check_form():
// <arguments> 	::= <element>, <checks> {,<element>, <checks>}
// <checks> 	::= <check>{,<check>}
// <check> 		::= 'required' | 'isMail' | 'isNumber' | 'inRange:<minValue>:<maxValue>' | 'radioSelectd' | 'minLength:<minValue>' | 'maxLength:<maxValue>'
// <minValue>	::= <integer>
// <maxValue>	::= <integer>
//
function check_form(element, checks) {
	var name, checks, check, checkargs, type, minValue, maxvalue, chk;
	
	// Loop for each pair of arguments: element checks
	for (i=0; i<(arguments.length-1); i+=2) {
		name = arguments[i];
		
		// Check if element exists
		if (document.getElementsByName(name).length < 1) {
			alert('Zu validierendes Element ' + name + ' nicht gefunden!'); // element not found
			return false;
		}
		
		checks = arguments[i+1];
		check = checks.split(',');
		
		// Loop for each check of an element
		for (j=0; j<(check.length); j+=1) {
			checkargs = check[j].split(':');
			type = checkargs[0];
			checkargs.length >= 2 ? minValue = checkargs[1] : minValue = 0;
			checkargs.length >= 3 ? maxValue = checkargs[2] : maxValue = 0;
			switch (type) {
				case 'required':		chk = check_required(name); break;
				case 'isMail': 			chk = check_isMail(name); break;
				case 'isNumber': 		chk = check_isNumber(name); break;
				case 'inRange':			chk = check_inRange(name, minValue, maxValue); break;
				case 'radioSelected':	chk = check_radioSelected(name); break;
				case 'minLength':		chk = check_minLength(name, minValue); break;
				case 'maxLength':		chk = check_maxLength(name, minValue); break;
				default:				chk = false; break;
			}
			
			// Handle an error
			if(chk == false) return(handle_error(name, type, minValue, maxValue));
		}
	}
	return true; // Return true if no errors detected
}

// Function handling an error
function handle_error(name, type, min, max) {
	var message;
	switch(type) {																											// English translation
		case 'required': 		message = name + ' muss angegeben werden!'; break; 											// is mandatory
		case 'isMail': 			message = name + ' ist keine gueltige E-Mail Adresse!'; break;								// is not a valid e-mail address
		case 'isNumber': 		message = name + ' muss numerisch sein!'; break;											// is not numeric
		case 'inRange': 		message = name + ' muss zwischen ' + min + ' und ' + max + ' sein!'; break;					// must be a value between min and max
		case 'radioSelected': 	message = name + ' muss ausgewaehlt werden!'; break;										// must be selected
		case 'minLength': 		message = name + ' muss mindestens ' + min + ' Stellen lang sein!'; break;					// must have a least min digits
		case 'maxLength': 		message = name + ' darf maximal ' + min + ' Stellen lang sein!'; break;						// can have max digits
		default:				message = 'Validierung von Element ' + name + ' als ' + type + ' ist unbekannt!'; break;	// unknown validation type for element
	}
	alert(message);
	point_to(name);
	return false;
}

// Sets focus on the element in error
function point_to(name) {
	document.getElementsByName(name)[0].focus();
}

// --------------------
// Validation functions
// --------------------

// Checks if a value is entered into a required element
function check_required(name) {
	var check = false;
	if(document.getElementsByName(name)[0].value.length > 0)
		check = true;
	return(check);
}

// Checks if an element contains a valid e-mail address
function check_isMail(name) {
	var check = true;
	var element_value = document.getElementsByName(name)[0].value;
	if(element_value.length > 0) {
		var atpos=element_value.indexOf("@");
		var dotpos=element_value.lastIndexOf(".");
		if (atpos<1 || dotpos<atpos+2 || dotpos+2>=element_value.length) 
			check = false;
	}
	return(check);
}

// Checks if the value of an element is a number
function check_isNumber(name) {
	var check = true;
	var element_value = document.getElementsByName(name)[0].value;
	if(element_value.length > 0) {
		if(isNaN(element_value))
			check = false;
	}
	return(check);
}

// Checks if the value of an element is a number and in a given range
function check_inRange(name, min, max) {
	var check = true;
	var element_value = document.getElementsByName(name)[0].value;
	if(element_value.length > 0) {
		element_value = parseInt(element_value, 10);
		if(isNaN(element_value) || element_value < min || element_value > max)
			check = false;
	}
	return(check);
}

// Check if one of a group of radio buttons is selected
function check_radioSelected(name) {
	var check = false;
	for(i = 0; i < document.getElementsByName(name).length; i++) {
		if(document.getElementsByName(name)[i].checked == true)
			check = true;
	}
	return(check);
}

// Check the value of an element is not longer than a given maximum
function check_maxLength(name, max) {
	var check = false;
	if(document.getElementsByName(name)[0].value.length <= max)
		check = true;
	return(check);
}

// Check the value of an element is at least as long as a given minimum
function check_minLength(name, min) {
	var check = false;
	if(document.getElementsByName(name)[0].value.length >= min)
		check = true;
	return(check);
}